package org.lee.export;

import org.lee.export.impl.PptExporter;
import org.lee.export.impl.PptExporterXSL;
import org.lee.export.model.ApiResult;
import org.lee.export.model.PptVersion;
import org.lee.export.model.ResultCode;
import org.lee.export.model.SourceType;
import org.lee.export.utils.FileUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author jackielee
 * @version 1.0
 * @description PptExportService
 * @date 2021/5/6
 **/
public class PptExportProxyService implements IPptExporter {

    private IPptExporter pptExporter;
    private IPptExporter pptExporterXSL;

    public PptExportProxyService() {
        pptExporter = new PptExporter();
        pptExporterXSL = new PptExporterXSL();
    }

    private ApiResult check(String fileName) {
        File file = new File(fileName);
        if (!FileUtils.checkPpt(file)) {
            return ApiResult.failure(ResultCode.INVALID, "the file to export for image was not ppt or pptx");
        }
        PptVersion version = getVersion(file);
        return ApiResult.success(version);
    }

    private PptVersion getVersion(File file) {
        String ext = FileUtils.getExt(file).trim();
        return ext.equalsIgnoreCase(PptVersion.PPT.getExtension()) ? PptVersion.PPT : PptVersion.PPTX;
    }

    @Override
    public ApiResult export(String fileName, String pathSave) {
        ApiResult res = check(fileName);
        if (res.getCode() != ResultCode.SUCCESS.getCode()) {
            return res;
        }
        return res.getData() == PptVersion.PPTX ? pptExporterXSL.export(fileName, pathSave) : pptExporter.export(fileName, pathSave);
    }

    @Override
    public ApiResult export(String path, String pathSave, SourceType sourceType) {
        if (sourceType == SourceType.FILE) {
            return export(path, pathSave);
        }
        List<File> files = FileUtils.getFiles(path);
        return exports(files, pathSave);
    }

    @Override
    public ApiResult export(List<String> files, String pathSave) {
        return exports(files.stream().map(f -> new File(f)).collect(Collectors.toList()), pathSave);
    }

    private ApiResult exports(List<File> files, String pathSave) {
        List<File> file2003 = new ArrayList<>();
        List<File> file2007 = new ArrayList<>();
        for (File file : files) {
            if (!FileUtils.checkPpt(file)) {
                continue;
            }
            PptVersion version = getVersion(file);
            if (version == PptVersion.PPT) {
                file2003.add(file);
            } else {
                file2007.add(file);
            }
        }
        ApiResult result2003, result2007;
        Map<String, List<String>> data = new Hashtable<>();
        if (!file2003.isEmpty()) {
            result2003 = pptExporter.export(file2003.stream().map(f -> f.getPath()).collect(Collectors.toList()), pathSave);
            if (result2003.getCode() == ResultCode.SUCCESS.getCode()) {
                data.putAll(result2003.getData());
            }
        }
        if (!file2007.isEmpty()) {
            result2007 = pptExporterXSL.export(file2007.stream().map(f -> f.getPath()).collect(Collectors.toList()), pathSave);
            if (result2007.getCode() == ResultCode.SUCCESS.getCode()) {
                data.putAll(result2007.getData());
            }
        }
        return data.isEmpty() ? ApiResult.failure(ResultCode.FAILURE, "export the files failure") : ApiResult.success(data);
    }
}
